﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net.Mail;
using Lenovo.CFI.Common.Mail;
using Lenovo.CFI.DAL.Mail;
using Lenovo.CFI.DAL;
using Lenovo.CFI.Common;
using Lenovo.CFI.Common.Sys;
using Lenovo.CFI.Common.Dic;
using Lenovo.CFI.BLL.DicMgr;

namespace Lenovo.CFI.BLL.Mail
{
    /// <summary>
    /// 需要封装各种Mail的模板
    /// </summary>
    public class MailBl
    {
        #region mail format and content

        public void MailTopicTdc(int topicID, string topicTitle, DateTime startTime, string mailTo)
        {
            string subject = String.Format("请准备CFI {0} 启动邮件内容！", topicTitle);

            string content = String.Format(@"你好！{0:yyyy-MM-dd}时间是{1}的启动时间，你是本TOPIC的负责人，请准备启动邮件内容，并在{2:yyyy-MM-dd}日期前提交给系统。谢谢!{3}",
                startTime, topicTitle, startTime.AddDays(-5), Utils.GetSysStatement());

            MailBl.SendMail(null, mailTo, null, subject, content, MailPriority.Normal);
        }


        public void MailTopicReady(int topicID, string topicTitle, string mailTo)
        {
            string subject = "启动材料准备完毕";
            string content = String.Format(@"你好!{0}准备材料已经准备完毕!", topicTitle);
            MailBl.SendMail(null, mailTo, null, subject, content, MailPriority.Normal);
        }


        public void MailTopicScheduleToOwner(int topicID, string topicTitle, string mailTo, string content)
        {
            string subject = "请准时参加CFI评审会！";
            MailBl.SendMail(null, mailTo, null, subject, content, MailPriority.Normal);
        }

        public void MailTopicScheduleToReviewer(int topicID, string topicTitle, string mailTo, string content)
        {
            string subject = "您被安排为CFI评审专家，请准时参加评审！";
            MailBl.SendMail(null, mailTo, null, subject, content, MailPriority.Normal);
        }




        public void MailSubmitReviewResult(string topicTitle, string reviewer)
        {
            string subject = String.Format("请尽快提交CFI {0} 评审结果！", topicTitle);
            string content = String.Format(@"谢谢!{0}", Utils.GetSysStatement());

            MailBl.SendMail(null, MailBl.ConvertMailAddress(reviewer), null, subject, content, MailPriority.Normal);
        }


        public void MailSuggestDcReject(string ideaTitle, string dcOwner, string mailTo)
        {
            string subject = String.Format("Idea {0}被{1}拒绝！", ideaTitle, dcOwner);
            string content = String.Format(@"你好！您分配的Idea{0}被{1}拒绝,请重新分配！{2}", ideaTitle, dcOwner, Utils.GetSysStatement());
            MailBl.SendMail(null, mailTo, null, subject, content, MailPriority.Normal);
        }

        public void MailSuggestDcFinish(string ideaTitle, string dcOwner, string suggestion, string mailTo)
        {
            string subject = String.Format("请您及时反馈Idea {0}的执行情况！", ideaTitle);
            string content = String.Format(@"你好！{0} 建议 Idea {1} 作 {2}，请及时反馈执行情况！{3}", dcOwner, ideaTitle, suggestion, Utils.GetSysStatement());
            MailBl.SendMail(null, mailTo, null, subject, content, MailPriority.Normal);
        }

        public void MailSuggestDcNotice(string ideaTitle, string dcOwner, string suggestion, string mailTo, string mailContent)
        {
            string subject = String.Format("请准备Idea {0}的详细介绍内容！", ideaTitle);
            string content = null;
            if (mailContent == null)
            {
                switch (suggestion)
                {
                    case NextAction.CPROJECT:
                    case NextAction.GROUPC:
                    case NextAction.PRODUCT:
                    case NextAction.JUSTDOIT:
                        content = String.Format(@"你好！感谢你参加本次CFI，{0} 建议你的Idea {1}作{2}，请你准备详细的介绍内容！{3}", dcOwner, ideaTitle,
                         NextActionDicMgr.dicMgr.GetTitle(suggestion), Utils.GetSysStatement());
                        break;
                    default:
                        return;
                }

            }
            else
                content = mailContent;

            MailBl.SendMail(null, mailTo, null, subject, content, MailPriority.Normal);
        }

        public void MailSuggestIpFinish(string ideaTitle, string dcOwner, string mailTo)
        {
            string subject = String.Format("请您及时反馈Idea {0}的执行情况！", ideaTitle);
            string content = String.Format(@"你好！{0} 建议 Idea {1} 作 IP，请及时反馈执行情况！{2}", dcOwner, ideaTitle, Utils.GetSysStatement());
            MailBl.SendMail(null, mailTo, null, subject, content, MailPriority.Normal);
        }


        public void MailActionExecution(int ideaID, string ideaTitle, string suggestion, string mailTo,
            DateTime gmoReportDate, DateTime mktReportDate, DateTime submitMaterialDate, string materialPrincipal)
        {
            string subject = null;
            string content = null;
            switch (suggestion)
            {
                case NextAction.CPROJECT:
                case NextAction.GROUPC:
                    subject = "CFI Idea评审结果通知";
                    content = String.Format(@"你好！感谢你参加本次CFI，你的IDEA {0} 通过评为的评审，建议作{1}，我们将在{2:yyyy-MM-dd}日向GMO汇报，请你准备详细的介绍内容，在{3:yyyy-MM-dd}日前提交给{4}!{5}",
                        ideaTitle, NextActionDicMgr.dicMgr.GetTitle(suggestion), gmoReportDate, submitMaterialDate, materialPrincipal, Utils.GetSysStatement());
                    break;
                case NextAction.PRODUCT:
                case NextAction.JUSTDOIT:
                    subject = "CFI Idea评审结果通知";
                    content = String.Format(@"你好！感谢你参加本次CFI，你的IDEA {0} 通过评为的评审，建议作{1}， 我们将在{2:yyyy-MM-dd}日向GMO汇报，请你准备好参加",
                        ideaTitle, NextActionDicMgr.dicMgr.GetTitle(suggestion), gmoReportDate);
                    break;
                case NextAction.OTHERBU:
                    subject = "CFI Idea评审结果通知";
                    content = String.Format(@"你好！这是来自于GDDL的和你们BU相关的IDEA {0}，请供参考！ 针对这些IDEA，你们的建议和后续意见，请反馈给我们。如果需要IDEA OWNER做进一步的沟通，可以联系我们。谢谢！{1}",
                        ideaTitle, Utils.GetSysStatement());
                    break;
                case NextAction.NEXTROUND:
                    subject = "CFI Idea评审结果通知";
                    content = String.Format(@"你好，由于没有参加本次评审，你的IDEA {0} 我们会安排在下次评审，请注意后续通知。谢谢!{1}",
                        ideaTitle, Utils.GetSysStatement());
                    break;
                case NextAction.MKT:
                    subject = "CFI Idea评审结果通知";
                    content = String.Format(@"你好！ 你的IDEA {0} 具备技术可行性，我们准备在{1:yyyy-MM-dd}和MKT沟通，请你准备进一步的详细内容，在{2:yyyy-MM-dd}日提交材料!{3}",
                        ideaTitle, mktReportDate, submitMaterialDate, Utils.GetSysStatement());
                    break;
                case NextAction.TBD:
                    subject = "CFI Idea评审结果通知";
                    content = String.Format(@"你好，感谢你参加CFI的活动，你的IDEA {0} 对我们未来的创新有一定的借鉴意义，它会被放入GDDL的Innovation Pool中，我们会定期REVIEW这些IDEA，期待后续与你的进一步沟通！谢谢!{1}",
                        ideaTitle, Utils.GetSysStatement());
                    break;
                default:
                    return;
            }


            MailBl.SendMail(null, mailTo, null, subject, content, MailPriority.Normal);
        }

        public void MailIPExecution(int ideaID, string ideaTitle, string mailTo,
            DateTime gmoReportDate, DateTime submitMaterialDate, string materialPrincipal)
        {
            string subject = null;
            string content = null;

            subject = "CFI Idea评审结果通知";
            content = String.Format(@"你好！感谢你参加本次CFI，你的IDEA {0} 通过评为的评审，建议作 IP，我们将在{1:yyyy-MM-dd}日向GMO汇报，请你准备详细的介绍内容，在{2:yyyy-MM-dd}日前提交给{3}!{4}",
                ideaTitle, gmoReportDate, submitMaterialDate, materialPrincipal, Utils.GetSysStatement());

            MailBl.SendMail(null, mailTo, null, subject, content, MailPriority.Normal);
        }

        public void MailSuggestion(string mailTo, DateTime suggestionDate, DateTime gmoReportDate, DateTime submitMaterialDate)
        {
            string subject = "您被安排为IDEA技术方向负责人，请准时提交反馈！";
            string content = String.Format(@"你好！ 请对你所负责的IDEA给出后续建议，并在{0:yyyy-MM-dd}日前反馈。对于需要做C、group C、product、just do it的IDEA，请让具体OWNER 准备详细的介绍内容，{1:yyyy-MM-dd}日提交材料，并在{2:yyyy-MM-dd}日向GMO汇报。{3}",
                            suggestionDate, submitMaterialDate, gmoReportDate, Utils.GetSysStatement());

            MailBl.SendMail(null, mailTo, null, subject, content, MailPriority.Normal);
        }


        #endregion

        #region mail format and content -- idea

        public void MailIdeaApproval(int ideaID, string ideaTitle, string ideaOwner, string mailTo)
        {
            string subject = String.Format("请对{0}提交的Idea {0} 进行审批！", ideaOwner, ideaTitle);
            string content = String.Format(@"你好！请对你{0}提交的IDEA {1} 进行审批，以便进行后续评审！{2}",
                            ideaOwner, ideaTitle, Utils.GetSysStatement());

            MailBl.SendMail(null, mailTo, null, subject, content, MailPriority.Normal);
        }


        #endregion


        #region Send

        internal static void SendMail(TranscationHelper trans, string to, string cc, string subject, string body, MailPriority priority)
        {
            #if DEBUG
            //屏蔽
            return;
            #endif

            if (String.IsNullOrEmpty(to)) return;

            Message message = new Message();
            message.From = "CFI";   // 由SP最终决定 -- 
            message.To = to;
            message.CC = cc;
            message.Subject = subject;
            message.Body = body;
            message.Priority = priority;

            if (trans != null)
                MessageDa.InsertMailQueue(message, trans);
            else
                MessageDa.InsertMailQueue(message);
        }

        // 兼容ITS系统的格式，未来可能变更
        internal static string ConvertMailAddress(params string[] itcodesOrEmail)
        {
            if (itcodesOrEmail == null || itcodesOrEmail.Length == 0) return "";
            StringBuilder sb = new StringBuilder();
            foreach (string to in itcodesOrEmail)
            {
                if (to.Contains("@"))
                    sb.AppendFormat("{0}\r\n", to);              // 去除t
                else
                    sb.AppendFormat("{0}@lenovo.com\r\n", to);   // 去除t
            }

            return sb.ToString();
        }

        // 兼容ITS系统的格式，未来可能变更
        internal static string ConvertMailAddress(ICollection<string> itcodesOrEmail)
        {
            if (itcodesOrEmail == null || itcodesOrEmail.Count == 0) return "";
            StringBuilder sb = new StringBuilder();
            foreach (string to in itcodesOrEmail)
            {
                if (to.Contains("@"))
                    sb.AppendFormat("{0}\r\n", to);              // 去除t
                else
                    sb.AppendFormat("{0}@lenovo.com\r\n", to);   // 去除t
            }

            return sb.ToString();
        }

        // 兼容ITS系统的格式，未来可能变更
        internal static string ConvertMailAddress(ICollection<User> users)
        {
            if (users == null || users.Count == 0) return "";
            StringBuilder sb = new StringBuilder();
            foreach (User to in users)
            {
                sb.AppendFormat("{0}@lenovo.com\r\n", to.Email);   // 去除t
            }

            return sb.ToString();
        }

        #endregion
    }
}
